<?php
namespace app\api\controller;
use think\Controller;
use think\Db;
use lib\Curl;
class Login extends Base
{
	//注册协议
	public function protocol(){
        $list=Db::name('setup')->where(array('id'=>1))->find();
        if($list){
            return json(['code' => 200, 'msg' => '查询成功' ,'data' =>$list]);
        }else{
            return json(['code' => 500, 'msg' => '查询失败', 'data' =>$list]);
        }
	}
	/**
	* 发送手机短信
	*/
	private function sendSmsByzk($phone, $content){
		if(!empty($phone) && !empty($content)){
			$content.="";
			$url="http://sdk.entinfo.cn:8061/webservice.asmx/mdsmssend?sn=SDK-FLH-010-00309&pwd=".strtoupper(md5('SDK-FLH-010-003092fEad6-f'))."&mobile=".$phone."&content=".$content."&ext=&stime=&rrid=&msgfmt=";
			$xml=Curl::http_curl($url);
			$xmlob=simplexml_load_string($xml);
			$arr=(array)$xmlob;
			
			if($arr[0] > 0){
				return true;
			}else{
				return false;
			}
		}else{
			return false;
		}
		
	}
	//发送短信接口
	public function sendSms(){
    	$phone=input("param.phone");
		if(!isset($phone) && empty($phone)){
			return json(['code' => 501, 'msg' => '手机号不能为空']);exit();
		}
		$time=60*12;			//几分钟后过期
		$verifyCode = rand(1000,9999);
		$content = "【卡帮帮】尊敬的用户您好，您的短信验证码为：".$verifyCode.",感谢您的使用，祝您生活愉快！";
		$result = Db::name('sms_log')->insertGetId(['phone' => $phone,'content' => $content,  'time' => time() + $time * 60, 'code' => $verifyCode]);
		if(!$result){
			return json(['code' => 502, 'msg' => '发送失败，稍后重试哟']);exit();
		}else{
			$res = $this->sendSmsByzk($phone,$content);
			if($res){
				return json(['code' => 200, 'msg' => '发送成功','data'=>$verifyCode]);exit();
			}else{
				return json(['code' => 503, 'msg' => '发送失败，稍后重试']);exit();
			}
		}
	}
	//注册
	public function register(){
		if(!empty(input("param.phone"))){
			$data['phone']=input("param.phone");
		}else{
			return json(['code' => 501, 'msg' => '手机号不能为空']);exit();
		}
		if(!empty(input("param.password"))){
			$data['password']=input("param.password");
		}else{
			return json(['code' => 502, 'msg' => '密码不能为空']);exit();
		}
		if(input("param.appname")){
			$data['appname']=input("param.appname");
		}
		$user=Db::name('users')->where(array('phone'=>$data['phone']))->find();
		if (empty($user)) {
			$data['password']=md5('tmd'.$data['password']);
			$data['time']=time();
			$data['status']=1;
			$id=Db::name('users')->insertGetId($data);
			if($id){
				$res['Token']=$this->do_mencrypt($id);
				return json(['code' => 200, 'msg' => '注册成功','data'=>$res]);exit();
			}else{
				return json(['code' => 504, 'msg' => '注册失败']);exit();
			}
		}else{
			return json(['code' => 503, 'msg' => '该手机号注册过了']);exit();
		}
	}
	//登陆
	public function login(){
		if(!empty(input("param.phone"))){
			$data['phone']=input("param.phone");
		}else{
			return json(['code' => 501, 'msg' => '手机号不能为空']);exit();
		}
		if(!empty(input("param.password"))){
			$data['password']=input("param.password");
		}else{
			return json(['code' => 502, 'msg' => '密码不能为空']);exit();
		}
		$user=Db::name('users')->where(array('phone'=>$data['phone']))->find();
		if (empty($user)) {
			return json(['code' => 503, 'msg' => '手机号不存在']);exit();
		}else{
			if($user['password']==md5('tmd'.$data['password'])){
				$res['Token']=$this->do_mencrypt($user['id']);
				Db::name('users')->where(array('phone'=>$data['phone']))->update(array('login_time'=>time()));
				return json(['code' => 200, 'msg' => '登陆成功','data'=>$res]);
				exit();
			}else{
				return json(['code' => 504, 'msg' => '密码错误']);exit();
			}
		}
	}
	//更改密码
	public function change_password(){
		if(!empty(input("param.phone"))){
			$data['phone']=input("param.phone");
		}else{
			return json(['code' => 501, 'msg' => '手机号不能为空']);exit();
		}
		if(!empty(input("param.password"))){
			$data['password']=input("param.password");
		}else{
			return json(['code' => 502, 'msg' => '密码不能为空']);exit();
		}
		$user=Db::name('users')->where(array('phone'=>$data['phone']))->find();
		if (empty($user)) {
			return json(['code' => 503, 'msg' => '手机号不存在']);exit();
		}else{
			$result=Db::name('users')->where(array('phone'=>$data['phone']))->update(array('password'=>md5('tmd'.$data['password']),'uptime'=>time()));
			if($result){
				$res['Token']=$this->do_mencrypt($user['id']);
				return json(['code' => 200, 'msg' => '更改成功','data'=>$res]);exit();
			}else{
				return json(['code' => 504, 'msg' => '更新失败']);exit();
			}
		}
	}
	//加密
	private function do_mencrypt($string, $key=''){
		$key= !empty($key) ? $key : config('byzk.KEY');
		$operation = 'E';
	   	$key=md5($key); 
	  	$key_length=strlen($key); 
	   	$string=$operation=='D'?base64_decode($string):substr(md5($string.$key),0,8).$string; 
	  	$string_length=strlen($string); 
	  	$rndkey=$box=array(); 
	  	$result=''; 
	  	for($i=0;$i<=255;$i++){
	  		$rndkey[$i]=ord($key[$i%$key_length]); 
	    	$box[$i]=$i; 
	  	} 
		for($j=$i=0;$i<256;$i++){ 
		    $j=($j+$box[$i]+$rndkey[$i])%256; 
		    $tmp=$box[$i]; 
		    $box[$i]=$box[$j]; 
		    $box[$j]=$tmp; 
		} 
		for($a=$j=$i=0;$i<$string_length;$i++){
		  	$a=($a+1)%256; 
		    $j=($j+$box[$a])%256; 
		    $tmp=$box[$a]; 
		    $box[$a]=$box[$j]; 
		    $box[$j]=$tmp; 
		    $result.=chr(ord($string[$i])^($box[($box[$a]+$box[$j])%256])); 
		}
		if($operation=='D'){
			if(substr($result,0,8)==substr(md5(substr($result,8).$key),0,8)){
				return substr($result,8); 
		    }else{
		    	return''; 
		    } 
		}else{ 
		    return str_replace('=','',base64_encode($result)); 
		} 
	}
}